import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = {
  user: null
}

const mutations = {
  setUser(state, user) {
    localStorage.setItem('user', JSON.stringify(user))
    state.user = user
  },
  logout(state){
    localStorage.removeItem('user')
    state.user = null
  }
}
const actions = {
  // 设置name
  setUser({ commit }, user) {
    commit('setUser', user)
  },

  logout({ commit }){
    commit('logout')
  }
}

const getters = {
  user: state => state.user
}

const store = new Vuex.Store({
  state,
  mutations,
  actions,
  getters
})

export default store
